<?php

/**
 * ECSHOP 淘宝商品批量导入程序
 * ============================================================================
 * 版权所有 2005-2009 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: item_extract.php 16881 2009-12-14 09:19:16Z gaoshikao $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

/* 权限检查 */
admin_priv('item_clear');

if (empty($_GET['is_ajax']))
{
    assign_query_info();
    $smarty->assign('ur_here', $_LANG['07_goods_clear']);
    $smarty->display('item_clear.htm');
}
else
{
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON();
    
    
    /* 设置最长执行时间为5分钟 */
    @set_time_limit(300);
    // 默认40张/页
	$page_size = 100; 
    $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $silent = empty($_GET['silent']) ? 0 : 1;

	/*获取本地商品总数*/
	$count = $db->getOne("SELECT COUNT(goods_id) FROM ".$ecs->table('goods'));
	
    if (isset($_GET['start']))
    {
		$title = sprintf($_LANG['item_format'], $count, $page_size);
		
        $result = array('error' => 0, 'message' => '', 'content' => '', 'done' => 1, 'title' => $title, 'page_size' => $page_size,
            'page' => 1, 'total' => 1, 'silent' => $silent,
            'row' => array('new_page'  => sprintf($_LANG['page_format'], 1),
                           'new_total' => sprintf($_LANG['total_format'], ceil($count/$page_size)),
                           'new_time'  => $_LANG['wait'],
                           'cur_id'    => 'time_1'));
        die($json->encode($result));
    }
    else
    {
        $start_time = gmtime(); //开始执行时间
        
	    /*获取本地商品*/
	    $sql = "SELECT goods_sn FROM ".$ecs->table('goods');
	    $res = $db->SelectLimit($sql, $page_size, ($page-1)*$page_size);
	    while($goods = $db->fetchRow($res)){ 
		    /* 检查是否重复 */
		    $num = $db->getOne("SELECT COUNT(num_iid) FROM ".$ecs->table('item')." WHERE num_iid = '$goods[goods_sn]'");
		    if ($num <= 0)
		    {
			    	$db->query("UPDATE ".$ecs->table('goods')." SET is_delete = '1' WHERE goods_sn='$goods[goods_sn]'");
		    }
		}
		
		$end_time = gmtime();
		
        $result = array('error' => 0, 'message' => '', 'content' => '', 'done' => 2);
        $result['page_size'] = $page_size;
        $result['page']      = $page;
        $result['total']     = $count;
        $result['silent']    = $silent;
        
        if ($result['silent'])
        {
            $err_msg = array();
        }
        
        /* 页数在许可范围内 */
        if ($result['page'] <= ceil($count / $result['page_size']))
        {
            $result['row']['pre_id'] = 'time_' . $result['page'];
            $result['row']['pre_time'] = ($end_time > $start_time) ? $end_time - $start_time : 1;
            $result['row']['pre_time'] = sprintf($_LANG['time_format'], $result['row']['pre_time']);
            $result['row']['cur_id'] = 'time_' . ($result['page'] + 1);
            $result['page']++; // 新行
            $result['row']['new_page'] = sprintf($_LANG['page_format'], $result['page']);
            $result['row']['new_total'] = sprintf($_LANG['total_format'], ceil($count/$result['page_size']));
            $result['row']['new_time'] = $_LANG['wait'];
            $result['total']++;
        }
        else
        {
            --$result['total'];
            --$result['page'];
            $result['done'] = 0;
            $result['message'] = $_LANG['done'];
            /* 清除缓存 */
            clear_cache_files();
            die($json->encode($result));
        }

        if ($result['silent'] && $err_msg)
        {
            $result['content'] = implode('<br />' , $err_msg);
        }

        die($json->encode($result));
    }
}

?>